package com.junior.fixclub.party;

import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Display;

import com.junior.fixclub.ParamMaker;

import de.robv.android.xposed.XC_MethodHook;
import de.robv.android.xposed.XposedBridge;
import de.robv.android.xposed.XposedHelpers;
import de.robv.android.xposed.callbacks.XC_LoadPackage;
import de.robv.android.xposed.callbacks.XCallback;

public class FixDisplay {
    public static String TAG = "FixClub";
    public void Fix(XC_LoadPackage.LoadPackageParam loadPkgParam){
        try {
            XposedHelpers.findAndHookMethod("android.view.Display", loadPkgParam.classLoader, "getMetrics", DisplayMetrics.class, new XC_MethodHook(XCallback.PRIORITY_LOWEST) {
                @Override
                protected void afterHookedMethod(MethodHookParam param) throws Throwable {
                    // TODO Auto-generated method stub
                    super.afterHookedMethod(param);
                    final int dpi = tryParseInt(ParamMaker.DPI);
                    DisplayMetrics metrics = (DisplayMetrics) param.args[0];
                    metrics.densityDpi = dpi;
                }
            });
        } catch (Exception e) {
            XposedBridge.log("Fake DPI ERROR: " + e.getMessage());
        }
        try {
            XposedHelpers.findAndHookMethod("android.view.Display", loadPkgParam.classLoader, "getRealMetrics", DisplayMetrics.class, new XC_MethodHook(XCallback.PRIORITY_LOWEST) {
                @Override
                protected void afterHookedMethod(MethodHookParam param) throws Throwable {
                    // TODO Auto-generated method stub
                    super.afterHookedMethod(param);
                    final int dpi = tryParseInt(ParamMaker.DPI);
                    DisplayMetrics metrics = (DisplayMetrics) param.args[0];
                    metrics.densityDpi = dpi;
                }
            });
        } catch (Exception e) {
            XposedBridge.log("Fake getRealMetrics ERROR: " + e.getMessage());
        }
        try {
            XposedHelpers.findAndHookMethod("android.view.Display", loadPkgParam.classLoader, "getMetrics", DisplayMetrics.class, new XC_MethodHook() {

                @Override
                protected void afterHookedMethod(MethodHookParam param) throws Throwable {
                    // TODO Auto-generated method stub
                    super.afterHookedMethod(param);
                    final float sdensity = ParamMaker.DENSITY;//"density";
                    DisplayMetrics metrics = (DisplayMetrics) param.args[0];
                    metrics.density = sdensity;
                }

            });
        } catch (Exception e) {
            XposedBridge.log("Fake getMetrics ERROR: " + e.getMessage());
        }


        try {
            XposedHelpers.findAndHookMethod("android.view.Display", loadPkgParam.classLoader, "getMetrics", DisplayMetrics.class, new XC_MethodHook() {
                @Override
                protected void afterHookedMethod(MethodHookParam param) throws Throwable {
                    // TODO Auto-generated method stub
                    super.afterHookedMethod(param);
                    final float sxdpi = ParamMaker.XDPI;//"xdpi"
                    DisplayMetrics metrics = (DisplayMetrics) param.args[0];
                    metrics.xdpi = sxdpi;
                }
            });
        } catch (Exception e) {
            Log.e(TAG,"Fake Real getMetrics ERROR: " + e.getMessage());
        }


        try {
            XposedHelpers.findAndHookMethod("android.view.Display", loadPkgParam.classLoader, "getMetrics", DisplayMetrics.class, new XC_MethodHook() {
                @Override
                protected void afterHookedMethod(MethodHookParam param) throws Throwable {
                    // TODO Auto-generated method stub
                    super.afterHookedMethod(param);
                    final float sydpi = ParamMaker.YDPI;//"ydpi"
                    DisplayMetrics metrics = (DisplayMetrics) param.args[0];
                    metrics.ydpi = sydpi;
                }
            });
        } catch (Exception e) {
            Log.e(TAG,"Fake Real getMetrics ERROR: " + e.getMessage());
        }


        try {
            XposedHelpers.findAndHookMethod("android.view.Display", loadPkgParam.classLoader, "getMetrics", DisplayMetrics.class, new XC_MethodHook() {
                @Override
                protected void afterHookedMethod(MethodHookParam param) throws Throwable {
                    // TODO Auto-generated method stub
                    super.afterHookedMethod(param);
                    final float scdensity =  ParamMaker.SCALED_DENSITY;//SharedPref.getfloatXValue("scaledDensity");
                    DisplayMetrics metrics = (DisplayMetrics) param.args[0];
                    metrics.scaledDensity = scdensity;
                }
            });
        } catch (Exception e) {
            Log.e(TAG,"Fake Real getMetrics ERROR: " + e.getMessage());
        }

        //  已废弃的修改屏幕信息
        XposedHelpers.findAndHookMethod("android.view.Display", loadPkgParam.classLoader, "getWidth", new XC_MethodHook() {
            @Override
            protected void afterHookedMethod(MethodHookParam param) throws Throwable {
                // TODO Auto-generated method stub
                super.afterHookedMethod(param);
                param.setResult(ParamMaker.WIDTH);//SharedPref.getintXValue("width")
            }
        });

        XposedHelpers.findAndHookMethod("android.view.Display", loadPkgParam.classLoader, "getHeight", new XC_MethodHook() {
            @Override
            protected void afterHookedMethod(MethodHookParam param) throws Throwable {
                // TODO Auto-generated method stub
                super.afterHookedMethod(param);
                param.setResult(ParamMaker.HEIGHT);//SharedPref.getintXValue("height")
            }
        });


        // 宽
        XposedHelpers.findAndHookMethod(Display.class, "getMetrics", DisplayMetrics.class, new XC_MethodHook(XCallback.PRIORITY_LOWEST) {
            protected void afterHookedMethod(MethodHookParam param) throws Throwable {
                final int zhenwidth = ParamMaker.WIDTH;//SharedPref.getintXValue("width");
                DisplayMetrics metrics = (DisplayMetrics) param.args[0];
                metrics.widthPixels =zhenwidth ;

            }
        });
        // 高
        XposedHelpers.findAndHookMethod(Display.class, "getMetrics", DisplayMetrics.class, new XC_MethodHook(XCallback.PRIORITY_LOWEST) {
            protected void afterHookedMethod(MethodHookParam param) throws Throwable {
                final int zhenheight = ParamMaker.HEIGHT;//SharedPref.getintXValue("height");
                DisplayMetrics metrics = (DisplayMetrics) param.args[0];
                metrics.heightPixels =zhenheight ;
            }
        });

    }


    private static int tryParseInt(String s) {
        try {
            return Integer.parseInt(s);
        } catch (NumberFormatException e) {
            return 320;
        }
    }

}
